/**
 * JSON Schema 相关类型定义
 */

// 基础字段类型
export const FIELD_TYPES = {
  STRING: 'string',
  NUMBER: 'number',
  INTEGER: 'integer',
  BOOLEAN: 'boolean',
  ARRAY: 'array',
  OBJECT: 'object'
}

// UI组件类型
export const UI_TYPES = {
  INPUT: 'input',
  TEXTAREA: 'textarea',
  SELECT: 'select',
  RADIO: 'radio',
  CHECKBOX: 'checkbox',
  SWITCH: 'switch',
  DATE_PICKER: 'date-picker',
  TIME_PICKER: 'time-picker',
  NUMBER_INPUT: 'number-input',
  SLIDER: 'slider',
  RATE: 'rate',
  UPLOAD: 'upload'
}

// 事件类型
export const EVENT_TYPES = {
  CHANGE: 'change',
  FOCUS: 'focus',
  BLUR: 'blur',
  CLICK: 'click',
  INPUT: 'input'
}

// 条件类型
export const CONDITION_TYPES = {
  EQUALS: 'equals',
  NOT_EQUALS: 'not_equals',
  GREATER_THAN: 'greater_than',
  LESS_THAN: 'less_than',
  CONTAINS: 'contains',
  NOT_CONTAINS: 'not_contains',
  IN: 'in',
  NOT_IN: 'not_in',
  IS_EMPTY: 'is_empty',
  IS_NOT_EMPTY: 'is_not_empty'
}

/**
 * 创建基础字段Schema
 * @param {object} options 字段配置
 * @returns {object} JSON Schema
 */
export function createFieldSchema(options = {}) {
  const {
    type = FIELD_TYPES.STRING,
    title = '',
    description = '',
    default: defaultValue = undefined,
    required = false,
    ui = {},
    events = {},
    conditions = [],
    validation = {}
  } = options

  const schema = {
    type,
    title,
    description
  }

  if (defaultValue !== undefined) {
    schema.default = defaultValue
  }

  // 验证规则
  if (validation.minLength !== undefined) schema.minLength = validation.minLength
  if (validation.maxLength !== undefined) schema.maxLength = validation.maxLength
  if (validation.minimum !== undefined) schema.minimum = validation.minimum
  if (validation.maximum !== undefined) schema.maximum = validation.maximum
  if (validation.pattern !== undefined) schema.pattern = validation.pattern
  if (validation.enum !== undefined) schema.enum = validation.enum

  // UI配置
  if (Object.keys(ui).length > 0) {
    schema['x-ui'] = ui
  }

  // 事件配置
  if (Object.keys(events).length > 0) {
    schema['x-events'] = events
  }

  // 条件配置
  if (conditions.length > 0) {
    schema['x-conditions'] = conditions
  }

  return schema
}

/**
 * 创建对象Schema
 * @param {object} properties 属性定义
 * @param {array} required 必填字段
 * @param {object} options 其他配置
 * @returns {object} JSON Schema
 */
export function createObjectSchema(properties = {}, required = [], options = {}) {
  return {
    type: FIELD_TYPES.OBJECT,
    properties,
    required,
    ...options
  }
}

/**
 * 创建数组Schema
 * @param {object} items 数组项Schema
 * @param {object} options 其他配置
 * @returns {object} JSON Schema
 */
export function createArraySchema(items = {}, options = {}) {
  return {
    type: FIELD_TYPES.ARRAY,
    items,
    ...options
  }
}